package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.my.ui.activity.LocationActiviy;

import me.goldze.mvvmhabit.bus.RxBus;

import static android.app.Activity.RESULT_OK;

/**
 * author : huang
 * date   : 2020/11/17 0017 10:32
 * version: 1.0
 */
public class MapLoctionModel extends ToolbarViewModel {
    LocationActiviy mActivity;
    private String address;

    public MapLoctionModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(LocationActiviy locationActiviy) {
        this.mActivity = locationActiviy;
        setTitleText("选择位置");
        setRightText("确定");
        setRightTextVisible(View.VISIBLE);
    }

    /**
     * 右边文字的点击事件，子类可重写
     */
    @Override
    protected void rightTextOnClick() {
        if (TextUtils.isEmpty(mActivity.title2)){
            ToastUtil.toastShortMessage("请选择地址");
            return;
        }
        Intent intent = mActivity.getIntent();
        intent.putExtra("latitude", mActivity.latLngEntity.getLatitude());
        intent.putExtra("longitude", mActivity.latLngEntity.getLongitude());
        if (!TextUtils.isEmpty(mActivity.address) && mActivity.address.contains("市")){
            address = mActivity.address.split("市")[0] + "市" + mActivity.title2;
            intent.putExtra("address", mActivity.address.split("市")[0] + "市" + mActivity.title2);
        }else {
             address = mActivity.title2;
            intent.putExtra("address",  mActivity.title2);
        }

        String mapInfo = mActivity.latLngEntity.getLatitude() + "-" + mActivity.latLngEntity.getLongitude() + "-" + address;
        mActivity.setResult(RESULT_OK, intent);
        RxBus.getDefault().post(EventConstants.MAP + "," + mapInfo);
        finish();
    }
}
